﻿Public Class DcargarDocumentos

    Public Shared Function SeleccionarTodo(ByVal descEmpresa As String, ByVal pCargarDcmto As EcargarDocumentos) As List(Of EcargarDocumentos)
        Dim loadDcmtos As List(Of EcargarDocumentos) = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_cargarDocumentos_sel", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@nroDocumento", SqlDbType.Char, 11).Value = pCargarDcmto.nroDocumento
                cmd.Parameters.Add("@codigoSeparacion", SqlDbType.Char, 9).Value = pCargarDcmto.codigoSeparacion
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            loadDcmtos = New List(Of EcargarDocumentos)
                            Dim loadDcmto As EcargarDocumentos = Nothing 'tipo 
                            Do While dr.Read
                                loadDcmto = New EcargarDocumentos( _
                                    dr.GetInt32(dr.GetOrdinal("idDcmto")), _
                                    dr.GetString(dr.GetOrdinal("nroDocumento")), _
                                    dr.GetString(dr.GetOrdinal("rutaDcmto")), _
                                    dr.GetString(dr.GetOrdinal("estado")), _
                                    dr.GetInt32(dr.GetOrdinal("idUsuario")), _
                                    dr.GetString(dr.GetOrdinal("NombreCompleto")), _
                                    dr.GetString(dr.GetOrdinal("nombreDocumento")), _
                                    dr.GetString(dr.GetOrdinal("fecha")), _
                                    dr.GetString(dr.GetOrdinal("idDcmtoRutaDcmto")), _
                                    dr.GetString(dr.GetOrdinal("codigoSeparacion")))
                                loadDcmtos.Add(loadDcmto)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return loadDcmtos
    End Function
    Public Shared Function Añadir(ByVal descEmpresa As String, ByVal pCargarDcmto As EcargarDocumentos) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_cargarDocumentos_add", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@nroDocumento", SqlDbType.Char, 11).Value = pCargarDcmto.nroDocumento
                cmd.Parameters.Add("@rutaDcmto", SqlDbType.Char, 200).Value = pCargarDcmto.rutaDcmto
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pCargarDcmto.idUsuario
                cmd.Parameters.Add("@nombreDocumento", SqlDbType.Char, 50).Value = pCargarDcmto.nombreDocumento
                cmd.Parameters.Add("@fecha", SqlDbType.Char, 10).Value = pCargarDcmto.fecha
                cmd.Parameters.Add("@codigoSeparacion", SqlDbType.Char, 9).Value = pCargarDcmto.codigoSeparacion
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Eliminar(ByVal descEmpresa As String, ByVal pCargarDcmto As EcargarDocumentos) As Boolean
        Dim resultado As Boolean = False
        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_cargarDocumentos_del", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idDcmto", SqlDbType.Int).Value = pCargarDcmto.idDcmto
                cmd.Parameters.Add("@codigoSeparacion", SqlDbType.Char, 11).Value = pCargarDcmto.codigoSeparacion
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pCargarDcmto.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function

End Class
